<?php

namespace app\admin\model;

/**
 *  退货退款单
 */
class Returnorder extends BaseModel
{
    protected $attributes = [
        'returnorder_type' => [
            1 => '仅退款',
            2 => '补偿款',
            3 => '退款且退货'
        ],
        'order_type' => [
            'wholesale' => '经销商订单',
            'order' => '普通订单'
        ],
        'status' => [
            1 => '审核中',
            2 => '待退货',
            3 => '待退款',
            4 => '已取消',
            5 => '已完成'
        ],
        'return_reason' => [
            1 => '产品损坏',
            2 => '质量问题',
            3 => '发错产品',
            4 => '其他原因'
        ],
        'returnorder_method' => [
            1 => '上门提货',
            2 => '客户寄送'
        ],
        'stock_type' => [
            1 => '正常入库',
            2 => '报损入库'
        ],
        'refund_status' => [
            '00' => '待审核',
            '01' => '退款成功',
            '03' => '待退款',
            '04' => '审核不通过',
        ],

    ];


    /**
     *  获取字段枚举
     */
    protected $enumCallbacks = [
        'operator' => 'getOperatorEnum'
    ];

    /**
     *  获取字段枚举
     */
    public function getFieldEnum($field)
    {
        return $this->getEnum($field, [], 'label');
    }

    /**
     *  获取创始人
     */
    public function getOperatorEnum()
    {
        return Operator::order('cname ASC')
            ->where('is_del', 0)
            ->where('status', 1)
            ->column('id as value,cname as label');
    }



    /**
     *  创始人
     */
    public function creatorName()
    {
        return $this->hasOne(Operator::class, 'id', 'creator');
    }


    /**
     *  客户名字
     */
    public function customerName()
    {
        return $this->hasOne(Customer::class, 'id', 'customer_id')->field('id,cusname')->bind(['customer_name' => 'cusname']);
    }

    /**
     *  关联商品明细
     */
    public function productsList()
    {
        return $this->hasMany(ReturnorderItem::class, 'returnorder_id', 'id');
    }

    /**
     *  关联订单
     */
    public function order()
    {
        return $this->hasOne(Order::class, 'id', 'order_id')->field('id,order_no,order_category')->bind(['order_no' => 'order_no', 'order_category' => 'order_category']);
    }

    /**
     * 签订人名字
     */
    public function signatory()
    {
        return $this->hasOne(Operator::class, 'id', 'signatory')->field('id,cname')->bind(['signatory_name' => 'cname']);
    }
}
